package com.example.auth.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.auth.entity.dto.AssignRoleDTO;
import com.example.auth.entity.dto.SysUserCreateDTO;
import com.example.auth.entity.dto.SysUserEditDTO;
import com.example.auth.entity.dto.SysUserQueryDTO;
import com.example.auth.entity.vo.SysUserQueryVO;
import com.example.auth.service.SysUserService;
import com.example.auth.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * @author: LiYang
 * @create: 2023-07-10 16:01
 * @Description:
 * @Version 1.0
 **/
@RestController
@RequestMapping("system/user")
@Api(tags = "用户管理")
@Validated
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    @PreAuthorize("hasAuthority('bnt.sysUser.list')")
    @PostMapping("getSysUserPageList/{pageNum}/{pageSize}")
    @ApiOperation("分页查询用户")
    public Result<Page<SysUserQueryVO>> getSysUserPageList(@PathVariable Long pageNum,
                                     @PathVariable Long pageSize,
                                     @RequestBody SysUserQueryDTO sysUserQueryDTO){
        Page<SysUserQueryVO> sysUserQueryVOPage = sysUserService.selectUserPage(pageNum, pageSize, sysUserQueryDTO);
        return Result.success(sysUserQueryVOPage);
    }

    @PreAuthorize("hasAuthority('bnt.sysUser.add')")
    @PostMapping("insert")
    @ApiOperation("新建用户")
    public Result<Void> insert(@RequestBody @Valid SysUserCreateDTO sysUserCreateDTO){
        sysUserService.insert(sysUserCreateDTO);
        return Result.success();
    }

    @PreAuthorize("hasAuthority('bnt.sysUser.list')")
    @ApiOperation("通过id查询用户")
    @GetMapping("getUserById/{id}")
    public Result<SysUserQueryVO> getUserById(@PathVariable Long id){
        SysUserQueryVO sysUserQueryVO = sysUserService.getUserById(id);
        return Result.success(sysUserQueryVO);
    }

    @PreAuthorize("hasAuthority('bnt.sysUser.update')")
    @ApiOperation("修改用户信息")
    @PostMapping("updateUser")
    public Result<Void> updateUser(@RequestBody @Valid SysUserEditDTO sysUserEditDTO){
        sysUserService.updateUser(sysUserEditDTO);
        return Result.success();
    }

    @PreAuthorize("hasAuthority('bnt.sysUser.remove')")
    @ApiOperation("通过id删除用户")
    @DeleteMapping("removeUserById/{id}")
    public Result<Void> removeUserById(@PathVariable Long id){
        sysUserService.removeUserById(id);
        return Result.success();
    }

    @PreAuthorize("hasAuthority('bnt.sysUser.remove')")
    @DeleteMapping("removeBatch")
    @ApiOperation("批量删除用户")
    public Result<Void> removeBatch(@RequestBody List<Long> idList) {
        sysUserService.removeBatch(idList);
        return Result.success();
    }

    @PreAuthorize("hasAuthority('bnt.sysUser.assignRole')")
    @ApiOperation(value = "根据用户id获取角色数据")
    @GetMapping("/toAssign/{userId}")
    public Result<Map<String,Object>> toAssign(@PathVariable Long userId) {
        Map<String, Object> roleMap = sysUserService.getRolesByUserId(userId);
        return Result.success(roleMap);
    }


    @PreAuthorize("hasAuthority('bnt.sysUser.assignRole')")
    @ApiOperation(value = "根据用户分配角色")
    @PostMapping("/doAssign")
    public Result<Void> doAssign(@RequestBody AssignRoleDTO assignRoleDTO) {
        sysUserService.doAssign(assignRoleDTO);
        return Result.success();
    }

    @GetMapping("updateUserStatus/{userId}/{status}")
    @ApiOperation("更改用户状态")
    public Result<Void> updateUserStatus(@PathVariable Long userId,
                                   @PathVariable Long status){
        sysUserService.updateUserStatus(userId,status);
        return Result.success();
    }

    @PreAuthorize("hasAuthority('bnt.sysUser.export')")
    @ApiOperation("导出用户信息Excel")
    @GetMapping("exportUserInfoExcel")
    public Result<Void> exportUserInfoExcel(HttpServletResponse response){
        sysUserService.exportUserInfoExcel(response);
        return Result.success();
    }


    @PreAuthorize("hasAuthority('bnt.sysUser.import')")
    @ApiOperation("导入用户信息Excel")
    @PostMapping("importUserInfoExcel")
    public Result<Void> importUserInfoExcel(MultipartFile file){
        sysUserService.importUserInfoExcel(file);
        return Result.success();
    }

}
